﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using Driver.Properties;
using MarketDataAnalyzer;

namespace Driver
{
    public class OutputUtilities
    {
        private static readonly string OutputDirectory = Settings.Default.OutputDir;

        public static void WriteAnalysesToFileHumanReadable(string fileName, IEnumerable<PriceAnalysis> analyses)
        {
            if (!Directory.Exists(OutputDirectory))
            {
                Directory.CreateDirectory(OutputDirectory);
            }

           // var fileName = string.Format("PriceAnalyses_{0}.txt", DateTime.UtcNow.ToString("yyyyMMdd-HH.mm.ss"));
            var fullPath = Path.Combine(OutputDirectory, fileName);
            var sb = new StringBuilder();

            foreach (var priceAnalysis in analyses)
            {
                sb.AppendLine( MarketDataAnalyzer.MarketDataAnalyzer.GetStandardPredictions( priceAnalysis ) );
            }

            File.WriteAllText( fullPath, sb.ToString() );
        }

        public static void WriteAnalysesToFileCsv(string fileName, IEnumerable<PriceAnalysis> analyses)
        {
            if (!Directory.Exists(OutputDirectory))
            {
                Directory.CreateDirectory(OutputDirectory);
            }

            //var fileName = string.Format( "PriceAnalyses_{0}.csv", DateTime.UtcNow.ToString( "yyyyMMdd-HH.mm.ss" ) );
            var fullPath = Path.Combine(OutputDirectory, fileName);

            File.WriteAllText( fullPath, ConvertAnalysesToCsv( analyses ) );
        }

        public static string ConvertAnalysesToCsv(IEnumerable<PriceAnalysis> priceAnalyses)
        {
            var sb = new StringBuilder();

            foreach (var priceAnalysis in priceAnalyses)
            {
                sb.AppendLine(MarketDataAnalyzer.MarketDataAnalyzer.GetStandardPredictionsCsv(priceAnalysis));
            }
            return sb.ToString();
        }
    }
}
